﻿using System;
using System.Net;
using System.IO;
using System.Xml;
using System.ComponentModel;
using FrameworkQ.WP7.Utility;

namespace BismillahReader.Core
{
    public class QuranTranslation : INotifyPropertyChanged
    {
        string _url = string.Empty;
        public QuranTranslation(string url)
        {
            _url = url;
            this.SurahTranslations = new System.Collections.Generic.Dictionary<int, SurahTranslation>();
        }

        public System.Collections.Generic.Dictionary <int, SurahTranslation> SurahTranslations { get; set; }

        private bool _isLoaded = false;
        private bool _isLoading = false;

        public bool IsLoaded
        {
            get { return _isLoaded; }
        }

        CachedInetAccess _net = new CachedInetAccess();
        public void Load()
        {
            _isLoading = true;
            _net.GetWebContent(_url, this, this.OnContent); 
        }

        public void OnContent(object sender, CachedInetAccess.ContentAvailableEventArgs args)
        {
            string[] lines = args.Result.Split("\n".ToCharArray());
            //int currentSura = -1;
            //int currentAyah = -1;

            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];
                string[] items = line.Split("|".ToCharArray());
                if (items.Length == 3)
                {
                    int surahNo = int.Parse(items[0]);
                    int ayatNo = int.Parse(items[1]);
                    string text = items[2];
                    if (!this.SurahTranslations.ContainsKey(surahNo))
                    {
                        SurahTranslation surahTranslation = new SurahTranslation()
                        {
                            No = surahNo,
                            Parent = this,
                            Name = QuranInfo.SurahNames[surahNo-1].Trim()
                        };
                        this.SurahTranslations.Add(surahNo, surahTranslation);
                    }
                    SurahTranslation surahTranslate = this.SurahTranslations[surahNo];
                    if (!surahTranslate.Ayats.ContainsKey(ayatNo))
                    {
                        AyahTranslation ayat = new AyahTranslation()
                        {
                            No = ayatNo,
                            Text = text.Trim(),
                            Parent = surahTranslate,
                            SuraNo = surahNo
                        };
                        surahTranslate.Ayats.Add(ayatNo, ayat);
                    }
                }
            }
            _isLoaded = true;
            _isLoading = false;
            if (this.OnLoadComplete != null)
            {
                OnLoadComplete(this);
            }
        }

        public delegate void LoadComplete (QuranTranslation translation);
        public event LoadComplete OnLoadComplete;

        private int _surahNo = 0;
        private int _ayatNo = 0;

        public int SurahNo
        {
            get { return _surahNo; }
            set
            {
                _surahNo = value;
                RaiseChange();
            }
        }

        private const string URL_TEMPLATE = "http://frameworkq.com/recite/?mp3=http://www.everyayah.com/data/Alafasy_128kbps/{0}{1}.mp3";
        private const string URL_TEMPLATE_ENGLISH = "http://frameworkq.com/recite/?mp3=http://www.everyayah.com/data/English/Ibrahim_Walk_192kbps_TEST/{0}{1}.mp3";
        private const string FORMAT_TEMPLATE = "000";

        public int AyatNo
        {
            get { return _ayatNo; }
            set
            {
                _ayatNo = value;
                RaiseChange();
            }
        }

        public static Uri GetUrl (int surahNo, int ayatNo, bool english)
        {
            string template = URL_TEMPLATE;
            if (english) { template = URL_TEMPLATE_ENGLISH; }
            return new Uri(string.Format(template, surahNo.ToString(FORMAT_TEMPLATE), ayatNo.ToString(FORMAT_TEMPLATE))); 
        }

        public Uri Url
        {
            get { return new Uri(string.Format(URL_TEMPLATE, _surahNo.ToString(FORMAT_TEMPLATE), _ayatNo.ToString(FORMAT_TEMPLATE))); }
        }

        public Uri EnglishUrl
        {
            get { return new Uri(string.Format(URL_TEMPLATE_ENGLISH , _surahNo.ToString(FORMAT_TEMPLATE), _ayatNo.ToString(FORMAT_TEMPLATE))); }
        }

        #region INotifyPropertyChanged Members

        private void RaiseChange()
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Url"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }

}
